<?php
/**
 * 系统设置
 * User: Administrator
 * Date: 2018/8/24 0024
 * Time: 16:34
 */
namespace app\publics\controller;

use app\common\model\ConfigModel;
use app\common\model\UserModel;
use think\facade\Request;
use think\Db;
use thinkcms\auth\Auth;
use think\facade\Cache;

class System extends Base
{
    private $c_model;

    public function __construct()
    {
        parent::__construct();
        $this->c_model = new ConfigModel();
    }

    // 常规设置
    public function general()
    {
        if (Request::isPost()) {
            $post = input('post.');
            $this->c_model->set_save($post, 'general', '常规设置');

            Cache::set('config', 0, 1);
            $auth = new Auth();
            $auth->createLog("管理员[ {$this->username} ]更新了系统管理=>系统设置=>常规设置", '更新数据');
            $this->success('操作成功');
        }

        $this->assign([
            'wxcode' => $this->c_model->get_value('wxcode'),
            'wxzh' => $this->c_model->get_value('wxzh'),
            'zfbcode' => $this->c_model->get_value('zfbcode'),
            'zfbzh' => $this->c_model->get_value('zfbzh'),
            'bank_name' => $this->c_model->get_value('bank_name'),
            'bank_account' => $this->c_model->get_value('bank_account'),
            'bank_people' => $this->c_model->get_value('bank_people'),
            'wxkf' => $this->c_model->get_value('wxkf'),
            'qqkf' => $this->c_model->get_value('qqkf'),
            'site_code' => $this->c_model->get_value('site_code'),
        ]);
        return $this->fetch();
    }

    // 管理员日志
    public function log()
    {
        $limit = empty(input('param.limit')) ? 10 : input('param.limit');
        $cur_page   = empty(input('param.page')) ? 1 : input('param.page');

        $query = [
            'limit' => $limit
        ];
        $count = (int)Db::name('action_log')->count();
        $list  = Db::name('action_log')
            ->order('create_time desc')
            ->paginate($limit, $count, [
                'query' => $query
            ]);

        $this->assign([
            'list'  => $list,
            'cur_page' => $cur_page,
            'limit' => select_limit($limit),
            'count' => $count,
            'page'  => $list->render()
        ]);
        return $this->fetch();
    }

    // 清空日志
    public function clearlog()
    {
        Db::name('action_log')->delete(true);
        $this->success('清空日志成功');
    }
}
